package com.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.AuctionDao;
import com.dao.FeedbackDao;
import com.dao.UserDao;
import com.mybatis.po.Auction;
import com.mybatis.po.Feedback;
import com.mybatis.po.User;
@Controller
public class AuctionController {
	@Autowired
	public UserDao userDao;
	@Autowired
	public AuctionDao auctionDao;
	@RequestMapping("/returnAction")
	@ResponseBody
	public Map<String, List<Auction>> returnAction(Auction auction) {
		System.out.print("这很好阿大大的"+auction.toString());
		Map<String,List<Auction>> map = new HashMap<String, List<Auction>>();
		List<Auction> list = auctionDao.selectAuctionByState(auction);
		map.put("arr", list);
		return map;
	}
	@RequestMapping("/returnActionCode")
	@ResponseBody
	public Map<String, List<Auction>> returnActionCode(Auction auction) {
		System.out.print("这很好阿大大的"+auction.toString());
		Map<String,List<Auction>> map = new HashMap<String, List<Auction>>();
		List<Auction> list = auctionDao.selectAuctionByBuySta(auction);
		map.put("arr", list);
		return map;
	}
	@RequestMapping("/offerAction")
	@ResponseBody
	public Map<String, List<Auction>> offerAction(Auction auction) {
		System.out.print("这很好阿大大的"+auction.toString());
		Map<String,List<Auction>> map = new HashMap<String, List<Auction>>();
		List<Auction> list = auctionDao.selectAuctionByPublisherAndState(auction);
		map.put("arr", list);
		return map;
	}
	@RequestMapping("/addAction")
	@ResponseBody
	public Map<String, String> addAction(Auction auction) {
		System.out.print("这很好阿大大的"+auction.toString());
		Map<String,String> map = new HashMap<String, String>();
		int list = auctionDao.addAction(auction);
		if(list == 1)map.put("state", "1");
		else {
			map.put("arr", "0");
		}
		return map;
	}
	@RequestMapping("/changeAction")
	@ResponseBody
	public Map<String, String> changeAction(Auction auction) {
		System.out.print("这很好阿大大的"+auction.toString());
		Map<String,String> map = new HashMap<String, String>();
		List<Auction> list = auctionDao.selectAuctionByCode(auction);
		List<Auction> list2 = auctionDao.selectAuctionByCode(auction);
		Auction auction2 = list.get(0);
		auction.setPrice(Integer.valueOf(auction2.getPrice())+auction.getPrice());
		int num = auctionDao.updateAction(auction);
		if(num == 1)map.put("state", "1");
		else {
			map.put("state", "0");
		}
		return map;
	}
	@RequestMapping("/sendAuction")
	@ResponseBody
	public Map<String, String> sendAuction(Auction auction) {
		System.out.print("这很好阿auction"+auction.toString());
		Map<String,String> map = new HashMap<String, String>();
		int list = auctionDao.updateActionByStatePu(auction);
		User userp = new User();
		userp.setId(auction.getPublisher());
		List<User> listUserP = userDao.selectUserById(userp);
		userp.setBalance(listUserP.get(0).getBalance()+auction.getPrice());
		int num = userDao.updateUserBlanceById(userp);
		User userb = new User();
		userb.setId(auction.getBuyer());
		List<User> listUserb = userDao.selectUserById(userb);
		userb.setBalance(listUserb.get(0).getBalance()-auction.getPrice());
		int num2 = userDao.updateUserBlanceById(userb);
		if(list == 1 && num == 1 && num2 == 1) map.put("state", "1");
		else {
			map.put("state", "0");
		}
		return map;
	}
}
